package com.qianbei.common;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianbei.R;


/**
 * 加载更多 LoadMoreListView
 *
 * @author jiaowezheng
 */
public class LoadMoreListView extends ListView implements OnScrollListener {

    public static final int STATUS_NORMAL = 1;          //正常状态
    public static final int STATUS_NO_DATA = 2;         //无数据状态
    public static final int STATUS_REFRESHING = 3;      //刷新状态

    private Context mContext;
    private int mRefreshStatus = STATUS_NO_DATA;
    private View mFooterLayout;
    private ProgressBar mProgressBar;
    private TextView mFooterTextView;
    private LinearLayout mLoadingLayout;

    private OnLoadMoreListViewListener onLoadMoreListViewListener;

    private int mNoDataHeight;
    private int mLoadingHeight;
    private int mHeight;

    private boolean isAverageHeight;

    public interface OnLoadMoreListViewListener {
        public void onLoadMore();
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(context);
    }

    public LoadMoreListView(Context context) {
        super(context);
        mContext = context;
        init(context);
    }


    private void init(Context context) {

        mNoDataHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,20,getResources().getDisplayMetrics());
        mLoadingHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,getResources().getDisplayMetrics());

        mFooterLayout = View.inflate(context,R.layout.footer_list_view,null);
        mProgressBar = (ProgressBar) mFooterLayout.findViewById(R.id.footer_progress_bar);
        mFooterTextView = (TextView) mFooterLayout.findViewById(R.id.footer_text);
        mLoadingLayout = (LinearLayout) mFooterLayout.findViewById(R.id.loading_layout);

        addFooterView(mFooterLayout);
        mFooterLayout.setVisibility(GONE);
        setOnScrollListener(this);
        setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    @Override
    public void onScroll(AbsListView l, int t, int oldl, int count) {
        if ((t + oldl) == count){
            //没有更多数据或者正在刷新状态
            if(mRefreshStatus == STATUS_NO_DATA || mRefreshStatus == STATUS_REFRESHING){
                return;
            }
            //只有在正常状态下才会执行刷新数据
            if(mRefreshStatus == STATUS_NORMAL){
                ListAdapter adapter = l.getAdapter();

                int defaultCount = getHeaderViewsCount() + 1;

                if (adapter != null && adapter.getCount() > defaultCount){
                    doMore();
                }
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView arg0, int arg1) {
    }

    public void setOnLoadMoreListViewListener(OnLoadMoreListViewListener onLoadMoreListViewListener) {
        this.onLoadMoreListViewListener = onLoadMoreListViewListener;
    }

    /**
     * 是否均分高度  1/2
     *
     * @param isAverage
     */
    public void setAverageHeight(boolean isAverage){
        this.isAverageHeight = isAverage;
    }

    /**
     * 开始加载更多
     */
    private void doMore(){
        if(onLoadMoreListViewListener != null){
            mRefreshStatus = STATUS_REFRESHING;
            onLoadMoreListViewListener.onLoadMore();
        }
    }

    /**
     * 加载更多完成之后调用
     */
    public void doneMore() {
        mRefreshStatus = STATUS_NORMAL;
        mFooterLayout.setVisibility(VISIBLE);
    }

    /**
     * 显示加载更多
     */
    public void showLoadMore(){
        mRefreshStatus = STATUS_NORMAL;
        mFooterLayout.setVisibility(VISIBLE);
        mLoadingLayout.setVisibility(VISIBLE);
        if (mProgressBar != null){
            mProgressBar.setVisibility(VISIBLE);
        }
        mFooterTextView.setText(R.string.loading);


    }
    /**
     * 显示加载更多并设置文字
     */
    public void showLoadMore(String text){
        mRefreshStatus = STATUS_NORMAL;
        mFooterLayout.setVisibility(VISIBLE);
        mLoadingLayout.setVisibility(VISIBLE);

        if (mProgressBar != null){
            mProgressBar.setVisibility(VISIBLE);
        }
        mFooterTextView.setText(text);
        setFooterLayoutHeight(1);
    }

    /**
     * 显示无更多数据设置文字
     * @param text
     */
    public void noMoreData(String text) {
        mRefreshStatus = STATUS_NO_DATA;
        mFooterLayout.setVisibility(View.VISIBLE);
        if (mProgressBar != null){
            mProgressBar.setVisibility(GONE);
        }
        mLoadingLayout.setVisibility(VISIBLE);
        mFooterTextView.setText(text);
    }

    /**
     * 无更多数据
     */
    public void noMoreData() {
        mRefreshStatus = STATUS_NO_DATA;
        mFooterLayout.setVisibility(View.VISIBLE);
        if (mProgressBar != null){
            mProgressBar.setVisibility(GONE);
        }
        mLoadingLayout.setVisibility(VISIBLE);
        mFooterTextView.setText("");

        int count = getHeaderViewsCount() + 1;
        mHeight = getHeight();

        if (getAdapter().getCount() == count){

            if (getHeaderViewsCount() > 0){
                View view  = getChildAt(0);
                if (view != null){
                    mHeight = getHeight() - view.getHeight();
                }
            }

            mFooterTextView.setText(R.string.empty_tip);
            setFooterLayoutHeight(3);
        }else{
            setFooterLayoutHeight(2);
        }

    }

    public void setFooterLayoutHeight(int type){
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLoadingLayout.getLayoutParams();

        if (type == 1){  //加载中
            params.height = mLoadingHeight;
        }else if (type == 2){ //数据加载完后，没有数据
            params.height = mNoDataHeight;
        }else{ //无数据
            if (isAverageHeight){
                mHeight = mHeight / 2;
            }
            params.height = mHeight;
        }

        mLoadingLayout.setLayoutParams(params);
    }

    public View getFooterView(){
        return mFooterLayout;
    }
}
